/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* **********************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Theme_Builder
{
    // TODO: move this functionality to the utility project

    internal class Validation
    {
        private delegate bool NumValidater( float value );

        private static bool IsNonNegative(float value)
        {
            return value >= 0.0f;
        }

        private static bool IsBetweenZeroAndOneHundred(float value)
        {
            return IsNonNegative( value ) && value <= 100.0f;
        }

        private static bool IsBetweenZeroAndThreeHundredSixty(float value)
        {
            return IsNonNegative(value) && value <= 360.0f;
        }

        private static bool IsBetweenZeroAndOneHundredTwenty(float value)
        {
            return IsNonNegative(value) && value <= 120.0f;
        }

        private static void ValidateNumber(object sender, CancelEventArgs e, NumValidater validater, string errMsg, string defValue)
        {
           string text = null;
           TextBox tb = null;

           if( sender.GetType() != typeof( System.Windows.Forms.TextBox ) )
           {
               MessageBox.Show("Expected Textbox in validation."); // Better to ASSERT here
               return;
           }

           // Typecast is safe because of above check.
           tb = (TextBox)sender;
           text = tb.Text;

           // Parse and validate the data in the textbox
           bool fValid = false;
           try
           {
               float parsedVal = float.Parse(text);
               fValid = validater(parsedVal);
           }
           catch( Exception /*ex*/ )
           {
              fValid = false; // Parse exception - fallthrough to the error handling.
           }

           if (!fValid)
           {
              MessageBox.Show(errMsg);
              tb.Text = defValue;
              e.Cancel = true;
           }
        }

        public static void Validate_BetweenZeroAndOneHundred(object sender, CancelEventArgs e)
        {
           ValidateNumber(sender, e, IsBetweenZeroAndOneHundred, "Please enter a number between 0 and 100.", "100");
        }

        public static void Validate_PositiveNumber(object sender, CancelEventArgs e)
        {
           ValidateNumber(sender, e, IsNonNegative, "Please enter a number greater than or equal to 0.", "0");
        }

        public static void Validate_BetweenZeroAnd360(object sender, CancelEventArgs e)
        {
            ValidateNumber(sender, e, IsBetweenZeroAndThreeHundredSixty, "Please enter a number between 0 and 360", "90");
        }

        public static void Validate_BetweenZeroAnd120(object sender, CancelEventArgs e)
        {
            ValidateNumber(sender, e, IsBetweenZeroAndOneHundredTwenty, "Please enter a number between 0 and 120", "0");
        }
    }
}
